const BaseIpcHandler = require('../BaseIpcHandler');

class AddRemoteHandler extends BaseIpcHandler {
    async handle(event, params) {
        // 解析参数
        // console.log(`>> AddRemoteHandler params = `, params);
        // let { name, address, port, username, password, identity_file } = params;

        // 业务逻辑
        let result = null;
        try {
            const lastId = await this.app.remote_db.addRemote(params);
            console.log(`Remote ${lastId} Created OK! params = `, params);
            result = { code: 0, msg: lastId };
        } catch (error) {
            console.error('Create Remote Failed, Error:', error.message);
            result = { code: -1, msg: error.message };
        }

        // 返回结果
        // console.log(`AddRemoteHandler result = `, result);
        if (event.from === 'localserver') {
            event.resp.send(result);
        } else {
            return result;
        }
    }
}

module.exports = AddRemoteHandler;
